package com.qf.purchasing.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import com.qf.purchasing.entity.Detail;
import com.qf.purchasing.entity.Post;
import com.qf.purchasing.mapper.DetailMapper;
import com.qf.purchasing.mapper.PostMapper;
import com.qf.purchasing.qo.PostQO;
import com.qf.purchasing.service.PostService;
import com.qf.purchasing.vo.PostVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class PostServiceImpl implements PostService {
    //flag[0] :增加修改标志  1：用户添加操作，2：用户删除操作
    //flag[1] :增加或修改的计划 id
    public static int[] flag = new int[2];


    @Resource
    private PostMapper PostMapper;

    @Resource
    private DetailMapper detailMapper;


    @Override
    public PageInfo<PostVO> query(int page, int size, String title, String year) {
        PageInfo<PostVO> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(
                () -> PostMapper.selectPost(title, year)
        );
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return pageInfo;
    }


    @Override
    public RespResult<PostVO> selectPostById(int PostId) {
        Post Post = PostMapper.selectById(PostId);
        if (!ObjectUtils.isEmpty(Post)) {
            PostVO PostVO = new PostVO();
            BeanUtils.copyProperties(Post, PostVO);
            return RespResult.success(ResultCode.SUCCESS, PostVO);
        } else {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Override
    public RespResult<String> insert(PostQO PostQO) {
        //查询此条明细是否存在，根据id判断
        Post PostResult = PostMapper.selectById(PostQO.getPostId());
        if (ObjectUtils.isEmpty(PostResult)) {
            //不存在，插入
            Post Post = new Post();
            BeanUtils.copyProperties(PostQO, Post);
            int count = PostMapper.insert(Post);
            if (count < 1) {
                throw new ServiceException(ResultCode.INSERT_ERROR);
            }

            flag[0] = 1;
            flag[1] = PostQO.getPostId();
            return RespResult.success("添加成功！");
        } else {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public RespResult<String> modify(PostQO PostQO) {
        Post Post = new Post();
        BeanUtils.copyProperties(PostQO, Post);
        int count = PostMapper.updateById(Post);
        if (count < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("记录更新成功！");
    }


    @Transactional
    @Override
    public RespResult<String> deletePostOne(int id) {
        int count = PostMapper.updateStatusById(id);
        if (count > 0) {
            //计划删除成功，把对应的采购明细也全部删除
            List<Detail> details = detailMapper.selectByScheduleId(id);
            if (!details.isEmpty()) {
                //如果计划存在明细，则删除所有明细
                count = detailMapper.updateStatusByScheduleId(id);
                if (count > 0) {

                    return RespResult.success("采购计划和对应明细均删除成功！");
                } else {
                    throw new ServiceException(ResultCode.DELETE_ERROR);
                }
            }
            return RespResult.success("删除成功,无采购明细！");
        } else {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }


    @Override
    public RespResult<String> deletePosts(int[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                deletePostOne(ids[i]);
            }
            return RespResult.success("删除成功");
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }


}
